from app.service.driver import Driver
from app.lib.HuaWeiDNS import HuaWeiDNS
from app.functions import *
from app.config import SysConfig


class Hw(Driver):
    def __init__(self, secret_id, secret_key, domain=""):
        self.DomainName = domain
        self.dnsPod = HuaWeiDNS({
            'ak': secret_id,
            'sk': secret_key
        })

    def addRecord(self, subDomain, recordValue):
        zoneRes = self.zoneInfo()
        if (zoneRes.get('status') != SysConfig.STATUS_CODE_SUCCESS):
            return zoneRes
        zoneId = zoneRes.get('data').get('id')
        return self.RecordCreate(zoneId, subDomain, recordValue, 'TXT')

    def cleanRecord(self, subDomain):
        zoneRes = self.zoneInfo()
        if (zoneRes.get('status') != SysConfig.STATUS_CODE_SUCCESS):
            return zoneRes
        zoneId = zoneRes.get('data').get('id')
        res = self.RecordList(zoneId, subDomain, 'TXT')
        if res.get('status') != SysConfig.STATUS_CODE_SUCCESS:
            return res
        records = res.get('data', {}).get('RecordList', [])
        for record in records:
            delRes = self.dnsPod.DeleteRecord(zoneId, record["id"])
            if delRes.get('status') != SysConfig.STATUS_CODE_SUCCESS:
                return delRes
        return funReturn(SysConfig.STATUS_CODE_SUCCESS, 'success')

    def zoneInfo(self):
        res = self.dnsPod.ListPublicZones({"name": self.DomainName})
        if (res.get('status') != SysConfig.STATUS_CODE_SUCCESS):
            return res
        list = res.get('data', {}).get('zones', [])
        zoneinfo = {}
        for zone in list:
            zoneName = zone.get("name", "").strip('.')
            if zoneName == self.DomainName:
                zoneinfo = zone
                break
        if not zoneinfo:
            return funReturn(SysConfig.STATUS_CODE_DATAERROR, self.DomainName + "Not exist.")
        return funReturn(SysConfig.STATUS_CODE_SUCCESS, 'success', zoneinfo)

    def RecordCreate(self, zoneId, subDomain, recordValue, recordType="TXT"):
        listRes = self.RecordList(zoneId, subDomain, recordType)
        if (listRes.get('status') == SysConfig.STATUS_CODE_SUCCESS):
            listData = listRes.get('data', {}).get('RecordList', [])
            for record in listData:
                self.dnsPod.DeleteRecord(zoneId, record["id"])
        value = '"' + recordValue + '"'
        recordName = subDomain + '.' + self.DomainName
        return self.dnsPod.CreateRecord(zoneId, recordName, recordType, value)

    def RecordList(self, zoneId, subDomain, recordType="TXT"):
        recordRes = self.dnsPod.DescribeRecordList(zoneId, {"subDomain": subDomain, "recordType": recordType})
        if (recordRes.get('status') != SysConfig.STATUS_CODE_SUCCESS):
            return recordRes
        list = recordRes.get('data', {}).get('recordsets', [])
        returnData = []
        for record in list:
            recordName = record.get("name", "").strip('.')
            if subDomain + "." + self.DomainName == recordName:
                returnData.append(record)
        return funReturn(SysConfig.STATUS_CODE_SUCCESS, 'success', {"RecordList": returnData})
